<template>
  <div class="login-page">
    <div class="login-container">
      <!-- 登录框 -->
      <div class="login-form">
        <!-- logo -->
        <img
          src="http://likede2-admin.itheima.net/img/logo.595745bd.png"
          alt=""
          class="login-logo"
        >
        <!-- form表单 -->
        <el-form
          ref="loginForm"
          :model="loginForm"
          status-icon
          :rules="loginRules"
        >
          <!-- 用户名 -->
          <el-form-item prop="username">
            <!-- <i class="el-icon-mobile-phone"></i> -->
            <el-input
              v-model="loginForm.username"
              prefix-icon="el-icon-mobile-phone"
              autocomplete="off"
              maxlength="18"
            />
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              show-password
              type="password"
              prefix-icon="el-icon-lock"
              autocomplete="off"
              maxlength="20"
            />
          </el-form-item>
          <!-- 验证码 -->
          <el-form-item prop="code">
            <el-row>
              <el-col :span="17">
                <el-input
                  v-model="loginForm.code"
                  prefix-icon="el-icon-document-checked"
                  placeholder="请输入验证码"
                  maxlength="4"
                />
              </el-col>
              <el-col :span="7">
                <img
                  :src="`http://likede2-admin.itheima.net/likede/api/user-service/user/imageCode/` + loginForm.randomCode "
                  alt=""
                  class="code-image"
                  @click="getRandomCode"
                >
              </el-col>
            </el-row>
          </el-form-item>
          <!-- 登录 -->
          <el-form-item>
            <el-button type="primary" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// import { login } from '@/api/user'
import nanoid from '@/utils/getRandom'
export default {
  name: 'LoginPage',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: 'admin',
        code: '',
        loginType: 0,
        randomCode: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入账户', trigger: 'blur' },
          { min: 1, max: 18, message: '限制输入1-18个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 1, max: 20, message: '限制输入1-20个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 1, max: 4, message: '限制输入1-4个字符', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    const res = await nanoid(32)
    this.loginForm.randomCode = res
  },
  methods: {
    async login() {
      await this.$refs.loginForm.validate()
      // console.log(this.loginForm)
      await this.$store.dispatch('user/login', {
        loginName: this.loginForm.username,
        password: this.loginForm.password,
        code: this.loginForm.code,
        loginType: this.loginForm.loginType,
        clientToken: this.loginForm.randomCode
      })
      // 需要异步操作存储token后再跳转
      this.$router.push('/')
    },
    async getRandomCode() {
      const res = await nanoid(32)
      this.loginForm.randomCode = res
      console.log(this.loginForm.randomCode)
    }
  }
}
</script>

<style lang="scss">
.login-container {
  position: relative;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  background: url(http://likede2-admin.itheima.net/img/background.be4fae7d.png)
    no-repeat;
  background-size: cover;
  .login-form {
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -259px;
    margin-top: -194px;
    padding: 76px 35px 0;
    width: 518px;
    height: 388px;
    background-color: #fff;
    box-shadow: 0 3px 70px 0 rgba(30, 111, 72, 0.35);
    border-radius: 10px;
    box-sizing: border-box;
    overflow: visible;
    line-height: 1.15;
    .el-form-item__content {
      line-height: 50px;
      position: relative;
      font-size: 14px;
      .el-input__inner {
        height: 50px;
      }
    }
  }
  .login-logo {
    position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -48px;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span:first-of-type {
      margin-right: 16px;
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: #889aa4;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: #999;
      margin: 0 auto 40px auto;
      text-align: center;
      font-weight: 700;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #889aa4;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }

  .code-image {
    display: block;
  }

  .el-button {
    width: 100%;
    height: 52px;
    background: linear-gradient(262deg, #2e50e1, #6878f0);
    opacity: 0.91;
    border-radius: 8px;
    color: #fff;
    text-shadow: 0 7px 22px #cfcfcf;
  }
}
</style>
